package com.dehui.traders.widgets;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.adapter.TableAdapter;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.Utils;

/**
 * Created by hqq on 18.4.25.
 */

public class TableView extends LinearLayout implements TableAdapter.OnTableData {

    protected List<MyHScrollView> mHScrollViews = new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    private int screenWidth;
    private View headView;
    private View lastSelectView;
    private String[] mFixes;
    private String[] mOthers;
    private Fragment mFragment;
    private TableAdapter mAdapter;

    public TableView(Context context) {
        super(context,null);
    }

    public TableView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    private void init() {
        this.setOrientation(LinearLayout.VERTICAL);
        inflater = LayoutInflater.from(context);
        screenWidth = Utils.getScreen(context).widthPixels;
        removeAllViews();
        intiTabHead();
        initTabContent();
    }

    private void intiTabHead(){
        headView = inflater.inflate(R.layout.tab_head_common, null);
        TextView tvHead0 = headView.findViewById(R.id.tv_head_0);
        TextView tvHead1 = headView.findViewById(R.id.tv_head_1);
        MyHScrollView scrollview = headView.findViewById(R.id.horizontal_scrollview);
        scrollview.setFragemnt(mFragment);
        LinearLayout llScroll = headView.findViewById(R.id.ll_scroll);
        tvHead0.setText(mFixes[0]);
        tvHead1.setText(mFixes[1]);
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) llScroll.getLayoutParams();
        lp.width = screenWidth / 4 * mOthers.length;
        llScroll.setLayoutParams(lp);
        mHScrollViews.add(scrollview);

        for (int i = 0; i < mOthers.length; i++){
            View item = inflater.inflate(R.layout.tab_head_item, null);
            TextView tvItem = item.findViewById(R.id.tv_item);
            RelativeLayout.LayoutParams tvlp = (RelativeLayout.LayoutParams) tvItem.getLayoutParams();
            tvlp.width = screenWidth / 4;
            tvItem.setLayoutParams(tvlp);
            tvItem.setText(mOthers[i]);
            llScroll.addView(item);
            tvItem.setOnClickListener(v -> {
                if (lastSelectView !=null)
                    lastSelectView.setSelected(false);
                tvItem.setSelected(true);
                lastSelectView = tvItem;
            });
        }

        tvHead1.setOnClickListener(v -> {
            if (lastSelectView !=null)
                lastSelectView.setSelected(false);
            tvHead1.setSelected(true);
            lastSelectView = tvHead1;
        });

        addView(headView);
    }

    private void initTabContent(){
        ConflictListView listView = new ConflictListView(context);
        mAdapter.setOnTableData(this);
        listView.setAdapter(mAdapter);
        addView(listView);
    }

    @Override
    public void addScrollviews(MyHScrollView scrollview) {
        if (!mHScrollViews.isEmpty()) {
            int size = mHScrollViews.size();
            MyHScrollView scrollView = mHScrollViews.get(size - 1);
            final int scrollX = scrollView.getScrollX();
            //第一次满屏后，向下滑动，有一条数据在开始时未加入
            if (scrollX != 0) {
                headView.post(()->{
                    scrollView.scrollTo(scrollX, 0);
                });
            }
        }
        mHScrollViews.add(scrollview);
    }

    public List<MyHScrollView> getScrollViews() {
        return mHScrollViews;
    }


    public TableView setFixes(String[] fixes){
        this.mFixes = fixes;
        return this;
    }

    public TableView setOthers(String[] others){
        this.mOthers = others;
        return this;
    }

    public TableView setFragment(Fragment fragment){
        this.mFragment = fragment;
        return this;
    }

    public TableView setAdapter(TableAdapter adapter){
        this.mAdapter = adapter;
        return this;
    }

    public void build(){
        init();
    }

}
